package isnork.g2;

import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.awt.geom.Point2D;

import isnork.sim.GameConfig;
import isnork.sim.Observation;
import isnork.sim.Player;
import isnork.sim.SeaLifePrototype;
import isnork.sim.iSnorkMessage;
import isnork.sim.GameObject.Direction;
import isnork.g2.G2Explorer;
import isnork.g2.utility.Utility;
import isnork.g7.SeaLifeComparator;
import java.util.PriorityQueue;

public class iSnorkConfig {
	
	String msg;
	int index;
	public static ArrayList<SeaLifePrototype> sorted_list;
	Map<SeaLifePrototype, Integer> top_creatures = new HashMap<SeaLifePrototype, Integer>();
	Map<Integer, SeaLifePrototype> top_creatures_indexwise = new HashMap<Integer, SeaLifePrototype>();
	SortedSet <SeaLifePrototype> queue_to_transmit = new TreeSet(new SeaLifeComparator());
	//int transmit_number = 0;
	//SortedSet <SeaLifePrototype> queue_to_view = new TreeSet(new SeaLifeComparator());
	//int to_view_number = 0;
	
	class Creatures{
		SeaLifePrototype creature;
		Point2D loc;
		//int time;
		double value;
		
		
		Creatures(){}
		
		Creatures (SeaLifePrototype s, Point2D d, double val)
		{
			this.creature = s;
			this.loc = d;
			this.value = val;
		}
	}
	
	Set<Creatures> queue_to_view = new HashSet<Creatures>();
	
		iSnorkConfig(G2Explorer diver)
	{	
		sorted_list = new ArrayList<SeaLifePrototype>(diver.seaLifePossibilites);
		Collections.sort(sorted_list, new SeaLifeComparator());
		//System.out.printf("Sorted\n");
		//for(int i=1; i<=sorted_list.size(); i++)
		{
			//System.out.printf("Creature number "+i+" with happiness"+sorted_list.get(i).getHappinessD()+"\n");
		}
		for(int i=0; i<26 && i < sorted_list.size(); i++)
		{
			top_creatures.put(sorted_list.get(i),i);
			top_creatures_indexwise.put(i, sorted_list.get(i));
		}
	}
	

	public String encode(SeaLifePrototype s)
	{
		int i = top_creatures.get(s);
		/*i = sorted_list.indexOf(s);
		for(int j=0; j<top_creatures.length; j++)
			if(!(top_creatures[j] == null) && top_creatures[j].equals(s))
				i=j;
			//else
			//	i=26;
		if (i == 0)
			return null;*/
		char c = (char) (i+96);
		msg = Character.toString(c);
		return msg;
	}
	
	public SeaLifePrototype decode(String s)
	{
		char c = s.charAt(0);
		int i = ((int)c)-96;
		if (i<=26)
			return top_creatures_indexwise.get(i);
		else
			return null;
		/*return sorted_list.get(i);
		if (i<=26)
			return top_creatures[i];
		else
			return null;*/
	}
	
	public String getMsgToTransmit(G2Explorer diver)
	{
		String msg = null;
		for (Observation b: diver.whatYouSee)
			//for (SeaLifePrototype s: top_creatures)
			for(int s=0; s<26; s++)
			{
				if (top_creatures_indexwise.isEmpty())
					break;				
				if( top_creatures.containsKey(s)  && (b.getName().equalsIgnoreCase(top_creatures_indexwise.get(s).getName())))
					queue_to_transmit.add(top_creatures_indexwise.get(s));
			}
		if(queue_to_transmit.size() != 0)
			msg = encode(queue_to_transmit.last());
						
		return msg;
	}
	
	public Creatures decodeReceivedMessages(G2Explorer diver)
	{
		SeaLifePrototype s = null;
		for(iSnorkMessage i: diver.incomingMessages)
		{
			s = decode(i.getMsg());
			//for(SeaLifePrototype x : top_creatures_indexwise)
			for(int x=0; x<26; x++)
			{
				if (top_creatures_indexwise.isEmpty())
					break;
				else
				{
					if(s.getName().equalsIgnoreCase(top_creatures_indexwise.get(x).getName()))
					{
						int times = diver.number_times_Species.get(top_creatures_indexwise.get(x));
						if(times < 3)
						{
							int v = (s.getHappiness()/((s.getMaxCount()+s.getMinCount())/2));
							Creatures c;
							if(times == 1)
								c = new Creatures(s,i.getLocation(),v*0.5);
							else if (times == 2)
								c = new Creatures(s,i.getLocation(),v*0.25);
							else
								c = new Creatures(s,i.getLocation(),v);
							queue_to_view.add(c);
						}
					}
				}
				}
		}
		
		Creatures max_c = null;
		double max = 0;
		Iterator<Creatures> iter = queue_to_view.iterator();
		Set<Creatures> to_be_deleted = new HashSet<Creatures>();
		//Iterator<Creatures> max = iter;
		
		while(iter.hasNext())
		{
			Creatures c = iter.next();
			if(c.value>max)
			{
				max = c.value;
				max_c = c;
			}
			if(c.creature.getSpeed() != 0)
				c.value=c.value-top_creatures.get(c.creature);
			if(c.value<=0)
				to_be_deleted.add(c);
			
			if(c.loc.equals(diver.location))
				to_be_deleted.add(c);				
		
		}
				
		iter = to_be_deleted.iterator();
		while(iter.hasNext()){
			queue_to_view.remove(iter.next());
		}
		
		return max_c;
		
		//for(Entry<SeaLifePrototype, Point2D> e : queue_to_view_point.entrySet())
	}
	
}